package com.huike.web.controller;

import com.huike.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

@RestController
public class FileController {


    @GetMapping("download")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
//
//        String fileUrl = String.valueOf(request.getRequestURL());
//        if (StringUtils.isBlank(fileUrl)) {
//            response.setHeader("Content-type", "text/html;charset=UTF-8");
//            String data = "文件下载失败";
//            OutputStream ps = response.getOutputStream();
//            ps.write(data.getBytes("UTF-8"));
//            return;
//        }
//        try {
//            // 拿到文件路径
//            String url = fileUrl.split("9000/")[1];
//            // 获取文件对象
//            MinioUtils minioUtils = new MinioUtils();
//            InputStream object = minioUtils.getObject("yxc", url.substring(url.indexOf("/") + 1));
//            byte buf[] = new byte[1024];
//            int length = 0;
//            response.reset();
//            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(url.substring(url.lastIndexOf("/") + 1), "UTF-8"));
//            response.setContentType("application/octet-stream");
//            response.setCharacterEncoding("UTF-8");
//            OutputStream outputStream = response.getOutputStream();
//            // 输出文件
//            while ((length = object.read(buf)) > 0) {
//                outputStream.write(buf, 0, length);
//            }
//            // 关闭输出流
//            outputStream.close();
//        } catch (Exception ex) {
//            response.setHeader("Content-type", "text/html;charset=UTF-8");
//            String data = "文件下载失败";
//            OutputStream ps = response.getOutputStream();
//            ps.write(data.getBytes("UTF-8"));
//        }
    }

}
